package com.yihen.controller;

import com.yihen.dto.Result;
import com.yihen.util.FileUtil;
import com.yihen.util.LevelCheck;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/logs")
@Slf4j
public class LogController {
    @GetMapping("/verify")
    public Result verify() {
        return LevelCheck.levelIsOK(2, "无法获取日志");
    }
    @GetMapping("/get/{level}/{date}")
    public void test(@PathVariable String level,@PathVariable Date date,HttpServletResponse response) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date_ = sdf.format(date);
        String fileName = level + "." + date_+ ".log";
        System.out.println(fileName);
        String filePath = "/home/yihen/IdeaProjects/UsedGoodsMarket/logger/" + fileName;
        FileInputStream fileInputStream = new FileInputStream(filePath);
        response.setContentType("application/exe");
        response.addHeader("Content-Disposition","attachment;filename="+fileName);
        IOUtils.copy(fileInputStream, response.getOutputStream());
    }




}